package com.demo.controller;

import com.demo.domain.User;
import com.demo.exception.RRException;
import com.demo.service.QualifierService;
import com.demo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author： yu.Tan
 * @Date: 2020/8/26 17:31
 * @Description:
 * @Api 注解可以用来标记当前Controller的功能
 * @ApiOperation 注解用来标记一个方法的作用。
 * @ApiImplicitParam 注解用来描述一个参数，可以配置参数的中文含义，也可以给参数设置默认值，这样在接口测试的时候可以避免手动输入,
 * 如果有多个参数，则需要使用多个@ApiImplicitParam注解来描述，多个@ApiImplicitParam注解需要放在一个@ApiImplicitParams注解中。
 * @ApiImplicitParam 注解中虽然可以指定参数是必填的，但是却不能代替@RequestParam(required = true)，
 * 前者的必填只是在Swagger2框架内必填，抛弃了Swagger2，这个限制就没用了，所以假如开发者需要指定一个参数必填，@RequestParam(required = true)注解还是不能省略
 */
@RestController
@Api(tags = "swagger测试相关接口")
@RequestMapping("/user")
@CrossOrigin //所有域名均可访问该类下所有接口
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    @Qualifier("qualifierServiceBImpl")
    private QualifierService qualifierService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation("添加用户接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名"),
            @ApiImplicitParam(name = "address", value = "地址", required = true)
    })
    public String addUser(@RequestParam("username") String username, @RequestParam("address") String address) {
        //模拟异常
        String s = null;
        s.length();
        return "yes";
    }


    @RequestMapping(value = "/get_id/{id}", method = RequestMethod.GET)
    @ApiOperation("根据id查询用户的接口")
    @ApiImplicitParam(name = "id", value = "用户id", required = true)
    public String getUserById(@PathVariable("id") Integer id) {

        //测试异常
        if (1 == 1) {
            throw new RRException("测试异常");
        }
        return "yes";
    }

    @RequestMapping(value = "/update_id", method = RequestMethod.POST)
    @ApiOperation("根据id更新用户的接口")
    public User updateUserById(@RequestBody User user) {
        return user;
    }


    @RequestMapping(value = "/test_list", method = RequestMethod.GET)
    @ApiOperation("测试返回集合")
    public List<Integer> testList() {
        List<Integer> list = new ArrayList();
        list.add(1);
        list.add(2);
        list.add(31);
        list.add(4);
        return list;
    }


    @RequestMapping(value = "/test_service", method = RequestMethod.GET)
    @ApiOperation("测试service返回集合")
    public List<Integer> testService() {
        return userService.testService();
    }
}
